<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
		"http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
	<title>Dialog Widget Automated (non-robot) Tests</title>

	<style type="text/css">
		@import "../../dojo/resources/dojo.css";
		@import "css/dijitTests.css";
	</style>

	<!-- required: a default dijit theme: -->
	<link id="themeStyles" rel="stylesheet" href="../../dijit/themes/claro/claro.css">

	<!-- required: dojo.js -->
	<script type="text/javascript" src="../../dojo/dojo.js"
		djConfig="parseOnLoad: true, isDebug: true"></script>

	<!-- functions to help test -->
	<script type="text/javascript" src="helpers.js"></script>

	<!-- not needed, for testing alternate themes -->
	<script type="text/javascript" src="_testCommon.js"></script>

	<script type="text/javascript">
		dojo.require("doh.runner");
		dojo.require("dijit.dijit"); // optimize: load dijit layer
		dojo.require("dijit.Dialog");

		dojo.addOnLoad(function(){
				// Non robot tests to see if nested dialogs work correctly
				// when obscured dialogs are destroyed/hidden, on race conditions of
				// multiple dialogs fading in/out at once, etc.
				doh.register("out-of-order dialog hide/destroy", [
					{
						name: "open first dialog",
						timeout: 10000,
						runTest: function(){
							var d = new doh.Deferred(),
								dlg1;

							// Start focus on a button on the page (although we aren't going
							// to click the button)
							dojo.byId("button").focus();

							// Create and show first dialog
							dlg1 = new dijit.Dialog({
								id: "dlg1",
								title: "dialog 1",
								content:
									"<input id='dlg1_inputA'><br>" +
									"<input id='dlg1_inputB'><br>" +
									"<input id='dlg1_inputC'><br>" +
									"<input id='dlg1_inputD'><br>" +
									"<input id='dlg1_inputE'><br>" +
									"<input id='dlg1_inputF'><br>" +
									"<input id='dlg1_inputG'><br>" +
									"<input id='dlg1_inputH'><br>"
							});
							dlg1.show();

							setTimeout(d.getTestCallback(function(){
								doh.t(isVisible(dlg1), "dialog 1 is visible");

								var dialog1Z = dojo.style(dlg1.domNode, "zIndex"),
									underlayZ = dojo.style(dojo.global.dijit._underlay.domNode, "zIndex");

								doh.t(dialog1Z > underlayZ, "dialog1 (zIndex=" + dialog1Z +
									") above underlay (zIndex=" + underlayZ + ")");

								doh.is("dlg1_inputA", dojo.global.dijit._curFocus.id, "focus is on the first field");
							}), 2000);

							return d;
						}
					},
					{
						name: "open second dialog",
						timeout: 20000,
						runTest: function(){
							var d = new doh.Deferred(),
								dlg1 = dijit.byId("dlg1"),
								dlg2;

							// Create and show second dialog
							dlg2 = new dijit.Dialog({
								id: "dlg2",
								title: "dialog 2",
								content:
									"<input id='dlg2_inputA'><br>" +
									"<input id='dlg2_inputB'><br>" +
									"<input id='dlg2_inputC'><br>" +
									"<input id='dlg2_inputD'><br>" +
									"<input id='dlg2_inputE'><br>" +
									"<input id='dlg2_inputF'><br>"
							});
							dlg2.show();

							setTimeout(d.getTestCallback(function(){
								doh.t(isVisible(dlg2), "dialog 2 is visible");

								var dialog1Z = dojo.style(dlg1.domNode, "zIndex"),
									dialog2Z = dojo.style(dlg2.domNode, "zIndex"),
									underlayZ = dojo.style(dojo.global.dijit._underlay.domNode, "zIndex");

								doh.t(underlayZ > dialog1Z, "underlay (zIndex=" + underlayZ +
									") above dialog1 (zIndex=" + dialog1Z + ")");
								doh.t(dialog2Z > underlayZ, "dialog2 (zIndex=" + dialog2Z +
									") above underlay (zIndex=" + underlayZ + ")");

								doh.is("dlg2_inputA", dojo.global.dijit._curFocus.id, "focus is on the first field");
							}), 2000);

							return d;
						}
					},
					{
						name: "destroy first dialog",
						timeout: 20000,
						runTest: function(){
							var d = new doh.Deferred(),
								dlg1 = dijit.byId("dlg1"),
								dlg2 = dijit.byId("dlg2");

							dlg1.destroy();

							setTimeout(d.getTestCallback(function(){
								doh.t(isVisible(dlg2), "dialog 2 is still visible");
								doh.t(isVisible(dojo.global.dijit._underlay), "underlay is still visible");

								doh.is("dlg2_inputA", dojo.global.dijit._curFocus.id, "dialog 2 still has focus");

								var dialog2Z = dojo.style(dlg2.domNode, "zIndex"),
									underlayZ = dojo.style(dojo.global.dijit._underlay.domNode, "zIndex");

								doh.t(dialog2Z > underlayZ, "dialog2 (zIndex=" + dialog2Z +
									") above underlay (zIndex=" + underlayZ + ")");

							}), 2000);

							return d;
						}
					},
					{
						name: "open third dialog",
						timeout: 20000,
						runTest: function(){
							var d = new doh.Deferred(),
								dlg2 = dijit.byId("dlg2"),
								dlg3;

							// Create and show third dialog
							dlg3 = new dijit.Dialog({
								id: "dlg3",
								title: "dialog 3",
								content:
									"<input id='dlg3_inputA'><br>" +
									"<input id='dlg3_inputB'><br>" +
									"<input id='dlg3_inputC'><br>" +
									"<input id='dlg3_inputD'><br>"

							});
							dlg3.show();

							setTimeout(d.getTestCallback(function(){
								doh.t(isVisible(dlg3), "dialog 3 is visible");

								// Even though a dialog was deleted, the zIndex of the dialog 3
								// should be above dialog 2.    This test is to make sure we don't
								// merely use _dialogStack.length to compute zIndex
								var dialog2Z = dojo.style(dlg2.domNode, "zIndex"),
									dialog3Z = dojo.style(dlg3.domNode, "zIndex"),
									underlayZ = dojo.style(dojo.global.dijit._underlay.domNode, "zIndex");
								doh.t(underlayZ > dialog2Z, "underlay (zIndex=" + underlayZ +
									") above dialog2 (zIndex=" + dialog2Z + ")");
								doh.t(dialog3Z > underlayZ, "dialog3 (zIndex=" + dialog3Z +
									") above underlay (zIndex=" + underlayZ + ")");

								doh.is("dlg3_inputA", dojo.global.dijit._curFocus.id, "focus is on the first field");
							}), 2000);

							return d;
						}
					},
					{
						name: "open fourth dialog",
						timeout: 30000,
						runTest: function(){
							var d = new doh.Deferred(),
								dlg3 = dijit.byId("dlg3"),
								dlg4;

							// Create and show fourth dialog
							dlg4 = new dijit.Dialog({
								id: "dlg4",
								title: "dialog 4",
								content:
									"<input id='dlg4_inputA'><br>" +
									"<input id='dlg4_inputB'>"
							});
							dlg4.show();

							setTimeout(d.getTestCallback(function(){
								doh.t(isVisible(dlg4), "dialog 4 is visible");

								var dialog3Z = dojo.style(dlg3.domNode, "zIndex"),
									dialog4Z = dojo.style(dlg4.domNode, "zIndex"),
									underlayZ = dojo.style(dojo.global.dijit._underlay.domNode, "zIndex");
								doh.t(underlayZ > dialog3Z, "underlay (zIndex=" + underlayZ +
									") above dialog3 (zIndex=" + dialog3Z + ")");
								doh.t(dialog4Z > underlayZ, "dialog4 (zIndex=" + dialog4Z +
									") above underlay (zIndex=" + underlayZ + ")");

								doh.is("dlg4_inputA", dojo.global.dijit._curFocus.id, "focus is on the first field");
							}), 2000);

							return d;
						}
					},
					{
						name: "hide third dialog",
						timeout: 40000,
						runTest: function(){
							var d = new doh.Deferred(),
								dlg3 = dijit.byId("dlg3"),
								dlg4 = dijit.byId("dlg4");

							dlg3.hide();

							setTimeout(d.getTestCallback(function(){
								doh.t(isVisible(dlg4), "dialog 4 is still visible");
								doh.t(isVisible(dojo.global.dijit._underlay), "underlay is still visible");

								doh.is("dlg4_inputA", dojo.global.dijit._curFocus.id, "dialog 4 still has focus");

								var dialog4Z = dojo.style(dlg4.domNode, "zIndex"),
									underlayZ = dojo.style(dojo.global.dijit._underlay.domNode, "zIndex");

								doh.t(dialog4Z > underlayZ, "dialog4 (zIndex=" + dialog4Z +
									") above underlay (zIndex=" + underlayZ + ")");

							}), 2000);

							return d;
						}
					},
					{
						name: "close fourth dialog",
						timeout: 30000,
						runTest: function(){
							var d = new doh.Deferred(),
								dlg2 = dijit.byId("dlg2"),
								dlg3 = dijit.byId("dlg3"),
								dlg4 = dijit.byId("dlg4");

							// Closing fourth dialog should move focus to second dialog,
							// since we already destroyed the third dialog
							dlg4.hide();

							setTimeout(d.getTestCallback(function(){
								doh.t(isHidden(dlg4), "dialog 4 is hidden");
								doh.t(isHidden(dlg3), "dialog 3 is hidden");
								doh.t(isVisible(dlg2), "dialog 2 is visible");

								var dialog2Z = dojo.style(dlg2.domNode, "zIndex"),
									underlayZ = dojo.style(dojo.global.dijit._underlay.domNode, "zIndex");
								doh.t(dialog2Z > underlayZ, "dialog2 (zIndex=" + dialog2Z +
									") above underlay (zIndex=" + underlayZ + ")");

								doh.is("dlg2_inputA", dojo.global.dijit._curFocus.id, "focus is on the first field");
							}), 2000);

							return d;
						}
					},
					{
						name: "close second dialog",
						timeout: 30000,
						runTest: function(){
							var d = new doh.Deferred(),
								dlg2 = dijit.byId("dlg2");

							// Since we already destroyed first dialog, closing second dialog should hide underlay and
							// revert focus to the main page
							dlg2.hide();

							setTimeout(d.getTestCallback(function(){
								doh.t(isHidden(dlg2), "dialog 4 is hidden");
								doh.t(isHidden(dojo.global.dijit._underlay), "underlay hidden");

								doh.is("button", dojo.global.dijit._curFocus.id, "focus is on the main page");
							}), 2000);

							return d;
						}
					}
				]);

				var dlgA, dlgB;
				doh.register("concurrent hide show", {
					name: "concurrent hide show",
					timeout: 20000,
					setUp: function(){
						// Create and show first dialog
						dlgA = new dijit.Dialog({
							id: "dlgA",
							title: "Dialog A",
							content:
								"<button type='button'>OK</button>"
						});
						dlgB = new dijit.Dialog({
							id: "dlgB",
							title: "dialog B",
							content:
								"<button type='button'>OK</button>"
						});
						dlgA.show();
						
					},
					runTest: function(){
						var d = new doh.Deferred(),
							cnt=0;

						handle = setInterval(d.getTestErrback(function(){
							var hidden = cnt%2 ? dlgA : dlgB,
								shown = cnt%2 ? dlgB : dlgA;
							
							if(cnt > 10){
								clearInterval(handle);
								handle = null;
								d.callback(true);
								return;
							}
							
							doh.t(isVisible(shown), shown.title + " visible");
							doh.t(isHidden(hidden), hidden.title + " hidden");
							doh.t(isVisible(dojo.global.dijit._underlay), "underlay visible");

							var shownZ = dojo.style(shown.domNode, "zIndex"),
								underlayZ = dojo.style(dojo.global.dijit._underlay.domNode, "zIndex");
							doh.t(shownZ > underlayZ, "visible dialog (zIndex=" + shownZ +
								") above underlay (zIndex=" + underlayZ + ")");

							hidden.show();
							shown.hide();

							cnt++;
						}), 1000);
						
						return d;
					},
					tearDown: function(){
						dlgA.hide();
						dlgB.hide();
						if(handle){
							clearInterval(handle);
						}
					}
				});
				doh.run();
			});
	</script>
</head>
<body class="claro">
	<h1 class="testTitle">Dijit Dialog Automated (non-robot) tests</h1>
	<button id="button">focus point</button>
</body>
</html>


